package com.chinairi.mall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chinairi.common.annotation.DoPage;
import com.chinairi.common.utils.RedisUtil;
import com.chinairi.mall.controller.viewobject.ContractMatVO;
import com.chinairi.mall.controller.viewobject.MaterialVO;
import com.chinairi.mall.dao.ContractMatMapper;
import com.chinairi.mall.dao.MatFeatureValueMapper;
import com.chinairi.mall.model.ContractMat;
import com.chinairi.mall.model.MatFeatureValue;
import com.chinairi.mall.model.Material;
import com.chinairi.mall.dao.MaterialMapper;
import com.chinairi.mall.service.IMaterialService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * <p>
 * 物料表 服务实现类
 * </p>
 *
 * @author gsl
 * @since 2020-08-26
 */
@Service
public class MaterialServiceImpl extends ServiceImpl<MaterialMapper, Material> implements IMaterialService {


    @Autowired
    private MaterialMapper materialMapper;

    @Autowired
    private ContractMatMapper contractMatMapper;

    @Autowired
    private MatFeatureValueMapper matFeatureValueMapper;
    @Autowired
    private RedisUtil redisUtil;

    /**
     * 新增单个物料
     *
     * @param materialVO
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addMaterial(MaterialVO materialVO) {
        materialVO.setMatDbid(UUID.randomUUID().toString().replace("-", ""));
        materialMapper.insert(materialVO);
        putRedis();
    }

    /**
     * 查询物料
     *
     * @param materialVO
     * @return
     */
    @Override
    @DoPage
    public List<Material> selectMaterial(MaterialVO materialVO) {
        QueryWrapper<Material> queryWrapper = new QueryWrapper<>();
        String matName = materialVO.getMatName();
        String matDbid = materialVO.getMatDbid();
        String matCode = materialVO.getMatCode();
        String matClassDbid = materialVO.getMatClassDbid();
        queryWrapper
                .like(matName != null && !matName.trim().isEmpty(), "MAT_NAME", matName)
                .eq(matDbid != null && !matDbid.trim().isEmpty(), "MAT_DBID", matDbid)
                .like(matCode != null && !matCode.trim().isEmpty(),"MAT_CODE",matCode)
                .eq(matClassDbid != null && !matClassDbid.trim().isEmpty(), "MAT_CLASS_DBID", matClassDbid);
        return materialMapper.selectList(queryWrapper);
    }

    /**
     * 根据主键获取物料
     *
     * @param contractMatVO
     * @return
     */
    @Override
    public MaterialVO getMat(ContractMatVO contractMatVO) {
        Material material = materialMapper.selectById(contractMatVO.getMatDbid());
        MaterialVO materialVO = new MaterialVO();
        BigDecimal percent = new BigDecimal("0.01");
        BigDecimal canBuyNum = contractMatVO.getCmMatQty().multiply(contractMatVO.getCmSetPercentage()).multiply(percent);
        materialVO.setGoodsCanBuy(canBuyNum.floatValue());
        materialVO.setCmSetPrice(contractMatVO.getCmSetPrice());
        BeanUtils.copyProperties(material,materialVO);
        return materialVO;
    }

    /**
     * 获取物料的特征值
     *
     * @param matDbid
     * @return
     */
    @Override
    public List<MatFeatureValue> getMatFeatureValue(String matDbid) {
        QueryWrapper<MatFeatureValue> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("MAT_DBID",matDbid);
        return matFeatureValueMapper.selectList(queryWrapper);
    }

    public void putRedis(){
        redisUtil.del("materialMap");
        List<Material> materialList = materialMapper.getIdAndName();
        Map<String,Object> materialMap = new HashMap<>(materialList.size());
        for (Material material : materialList){
            materialMap.put(material.getMatDbid(),material.getMatName());
        }
        redisUtil.hmset("materialMap", materialMap, 86500);
    }
}
